package com.ziyun.erp.web.modules.member.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.member.entity.MemRechargeVO;
import com.ziyun.erp.modules.member.entity.UserBaseInfoEntity;
import com.ziyun.erp.modules.member.service.*;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 用户基础信息表
 *
 * @author jiangli
 * @email
 * @date 2018-08-01 09:31:39
 */
@RestController
@RequestMapping("member/userbaseinfo")
public class UserBaseInfoController {

    @Autowired
    private UserBaseInfoService userBaseInfoService;
    @Autowired
    private UserAdvanceService userAdvanceService;
    @Autowired
    private UserCreditService userCreditService;
    @Autowired
    private UserPointService userPointService;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    private SysUserService sysUserService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:userbaseinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        return userBaseInfoService.list(params);
    }

    @RequestMapping("/listMall")
    public R listMall(@RequestParam Map<String, Object> params) {
        return userBaseInfoService.listMall(params);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("member:userbaseinfo:info")
    public R info(@PathVariable("userId") Integer userId) {
        Map<String, Object> userBaseInfo = userBaseInfoService.selectByIda(userId);
        return R.ok().put("userBaseInfo", userBaseInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("member:userbaseinfo:save")
    public R save(@RequestBody UserBaseInfoEntity userBaseInfo) {
        userBaseInfoService.insert(userBaseInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:userbaseinfo:update")
    public R update(UserBaseInfoEntity userBaseInfo) {
        userBaseInfoService.updateUserBaseInfo(userBaseInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:userbaseinfo:delete")
    public Map<String, Object> delete(String ids) {
        return userBaseInfoService.delete(ids);
    }

    /**
     * 会员充值
     */
    @RequestMapping("/memRecharge")
    public Map<String, Object> memRecharge(MemRechargeVO memRecharge) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        return userBaseInfoService.memRecharge(memRecharge, curentUser);
    }

    /**
     * 新增会员
     */
    @RequestMapping("/addMem")
    public R addMem(String name, String password, String mobile) {
        return userBaseInfoService.addMem(name, password, mobile);
    }

    /**
     * 重置密码
     */
    @RequestMapping("/resetPassword")
    public Map<String, Object> resetPassword(Integer userId,String password) {
        return userBaseInfoService.resetPassword(userId,password);
    }

    /**
     * 查询预存款记录
     */
    @RequestMapping("/advanceList/{userId}")
    public R advanceList(@RequestParam Map<String, Object> params, @PathVariable("userId") Integer userId) {
        PageUtils page = userAdvanceService.getAll(params, userId);

        return R.ok().put("page", page);
    }

    /**
     * 查询信用支付记录
     */
    @RequestMapping("/creditList/{userId}")
    public R creditList(@RequestParam Map<String, Object> params, @PathVariable("userId") Integer userId) {
        PageUtils page = userCreditService.queryPage(params, userId);

        return R.ok().put("page", page);
    }

    /**
     * 查询积分记录
     */
    @RequestMapping("/pointList/{userId}")
    public R pointList(@RequestParam Map<String, Object> params, @PathVariable("userId") Integer userId) {
        PageUtils page = userPointService.queryPage(params, userId);

        return R.ok().put("page", page);
    }

    /**
     * 查询订单记录
     */
    @RequestMapping("/orderList/{userId}")
    public R orderList(@RequestParam Map<String, Object> params, @PathVariable("userId") Integer userId) {
        PageUtils page = orderInfoService.getAllOrderInfo(params, userId);

        return R.ok().put("page", page);
    }

    /**
     * 查询收货地址
     */
    @RequestMapping("/addressList/{userId}")
    public R addressList(@RequestParam Map<String, Object> params, @PathVariable("userId") Integer userId) {
        PageUtils page = userAddressService.queryPage(params, userId);

        return R.ok().put("page", page);
    }

    /**
     * 查询所属客服adminUserIdList
     */
    @RequestMapping("/adminUserIdList")
    public R adminUserIdList() {
        List<SysUserEntity> adminUserIdList = sysUserService.selectList(new EntityWrapper<SysUserEntity>().eq("status", 1));

        return R.ok().put("adminUserIdList", adminUserIdList);
    }


}
